my $devenv='F:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe';
my $boost='F:\boost_1_51_0';
my $directx='F:\Program Files (x86)\Microsoft DirectX SDK (June 2010)';


my $boost_lib=$boost.'\stage\lib';
my $directx_include=$directx.'\include';
my $directx_lib=$directx.'\lib';
my $directx_effect_include=$directx.'\Samples\C++\Effects11\Inc';
my $directx_effect_lib=$directx.'\Samples\C++\Effects11\Debug';
my $ext_include='..\..\ext\Lua\include;..\..\ext\TinyXML';
my $ext_lib='..\..\ext\Lua\lib';

my $xmlconverter='../sdk/Debug/XmlConverter.exe';
my $res='../res/';

sub ConvertBracket
{
	my $tmp=shift;
	$tmp=~s/\(/%28/sg;
	$tmp=~s/\)/%29/sg;
	return $tmp;
}

print "DirectX include path: ".$directx_include."\n";
print "DirectX lib path: ".$directx_lib."\n";
print "DirectX Effect include path: ".$directx_effect_include."\n";
print "DirectX Effect lib path: ".$directx_effect_lib."\n";
print "Boost path: ".$boost."\n";
print "Boost lib path: ".$boost_lib."\n";

$directx_include=ConvertBracket($directx_include);
$directx_lib=ConvertBracket($directx_lib);
$directx_effect_include=ConvertBracket($directx_effect_include);
$directx_effect_lib=ConvertBracket($directx_effect_lib);

my $include_context='<AdditionalIncludeDirectories>REPLACE%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>';
my $lib_context='<AdditionalLibraryDirectories>REPLACE%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>';

$include_context=~s/REPLACE/${directx_include};${directx_effect_include};${boost};${ext_include};/;
$lib_context=~s/REPLACE/${directx_lib};${directx_effect_lib};${boost_lib};${ext_lib};/;

sub GetContext
{
	my $filename=shift;
	
	my $context=`cat ${filename}`;
	
	return $context;
}

sub UpdateEnvironment
{
	my $filename=shift;
	my $storefilename=$filename.".bak";
	my $context=GetContext($filename);
	$context=~s/<AdditionalIncludeDirectories>.*<\/AdditionalIncludeDirectories>/$include_context/sig;
	$context=~s/<AdditionalLibraryDirectories>.*<\/AdditionalLibraryDirectories>/$lib_context/sig;
	open(FH,">$filename");
	print FH $context;
	close FH;
}

UpdateEnvironment("../sdk/Engine/Engine.vcxproj");
UpdateEnvironment("../sdk/XmlConverter/XmlConverter.vcxproj");
UpdateEnvironment("../sdk/MD5Converter/MD5Converter.vcxproj");

print "Build XmlConverter...\n";
#`\"$devenv\" \"../sdk/Engine.sln\" -Rebuild \"Debug\" -Project XmlConverter`;
print "Build MD5Converter...\n";
#`\"$devenv\" \"../sdk/Engine.sln\" -Rebuild \"Debug\" -Project MD5Converter`;
print "Build Engine...\n";
#`\"$devenv\" \"../sdk/Engine.sln\" -Rebuild \"Debug\" -Project Engine`;

if(! -d "../sdk/Debug/log")
{
	`mkdir \"../sdk/Debug/log\"`;
}

print "Perpare Data...\n";

my @files=`find \"$res\" -name "*.xml" -prune -o -type f -print`;

my @files2=`find \"$res\" -name "*.xml"`;
foreach my $file(@files2)
{
	if($file!~/resmap.xml/)
	{
		$file=~s/[\r\n]//sg;
		print "$file\n";
		`\"$xmlconverter\" $file`;
	}
}

my $resmapBanner='<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<FILE name=\"resmap\" major_version=\"1\" minor_version=\"1\">
	<ChunkBlock name=\"resmap\">
';
	
my $resmapFooter='	</ChunkBlock>
</FILE>';

my $resmapBody='		<string name=\"map\">
			RESNAME
			RESPATH
		</string>
';
		
my $Body="";

foreach my $file(@files)
{
	$file=~s/[\r\n]//sg;
	if($file!~/run.pl/&&
		$file!~/resmap.data/)
	{
		my $resname=$file;
		$resname=~s/.*\/(.*?)/$1/;
		my $context=$resmapBody;
		$context=~s/RESNAME/$resname/;
		$file=~s/\.\./\./;
		$context=~s/RESPATH/$file/;
		$Body.=$context;
	}
}

my $resContext=$resmapBanner . $Body . $resmapFooter;
print "Convert resmap.xml\n";
`echo "$resContext" > \"../res/resmap.xml\"`;

`\"$xmlconverter\" \"../res/resmap.xml\"`;

`cp \"../res\" \"../sdk/Debug/\" -R`;

chdir "../sdk/Debug";

`\"Engine.exe\"`;